<?php
class Baogg_Controller_Action_Helper_Widget extends Zend_Controller_Action_Helper_Abstract
{
	private static $smarty;
	public function __construct ()
	{
	}
	public static function getSmarty()
	{
		if (!isset(self::$smarty)) {
			$smarty = new Smarty();
			$smarty_cfg = array('debugging' => 1,
	            'left_delimiter' => "{", 
	            'right_delimiter' => "}",
	            'compile_check' => 1,
	            'force_compile' => 1, 
	            'caching' => 0,
	            'cache_lifetime' => 0, 
	            'SMARTY_DIR' => BAOGG_ROOT . 'library/smarty/', 
	            'template_dir' => BAOGG_ROOT . 'views/widgets', 
	            'compile_dir' => BAOGG_ROOT . 'views/widgets_c', 
	            'plugins_dir' => array(BAOGG_ROOT . 'library/smarty/plugins'));
	
			foreach ($smarty_cfg as $key => $value) {
				$smarty->$key = $value;
			}
			self::$smarty = $smarty;
		}
		return self::$smarty;
	}
	
	public function loadWidget ($id, $options = array())
	{
		$ret = "";

		$smarty=self::getSmarty();
		$smarty->clearAllAssign();
		if($options && is_array($options)){
			$smarty->assign($options);
		}
		return $smarty->fetch($id . '.tpl');
	}

	/* public function getData($id,$options=array()){
		$class='Widget_'.$this->fixedId($id);

		$params=array_merge($this->getRequest()->getParams(),$options);
		//$instance=new $class;
		$arr_info= $class::getData($options);
		return $arr_info;
	}

	public function getJson($id, $options = array()){
		$class='Widget_'.$this->fixedId($id);

		$params=array_merge($this->getRequest()->getParams(),$options);
		//$instance=new $class;
		$arr_info= $class::getData($options);
		 

		 
		$page=@$params['page'];
		$limit=@$params['rows'];
		$sidx=@$params['sidx'];
		$sord=@$params['sord'];
		$count=count((array)$arr_info);

		//echo '<pre>';print_r($this->getRequest()->getParams());print_r($options);exit;var_dump($params);exit;
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		 
		$response=new stdClass();
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		
		//echo '<pre>';print_r(array_slice($arr_info, $limit*$page-$limit,$limit));echo $limit;echo $limit*$page-$limit;exit;
		$response->rows=array_slice($arr_info, $limit*$page-$limit,$limit);

		return $response;
	}
	*/
	private function fixedId($id){
		$id= preg_replace("/[^a-z0-9]+/"," ",strtolower($id));
		$id=ucwords($id);
		return str_replace(" ","",$id);
	}

	public function __call($method, $args)
	{
		$id=$args[0];
		$options=isset($args[1])?$args[1]:array();

		$class='Widget_'.$this->fixedId($id);
		$params=array_merge($this->getRequest()->getParams(),$options);
		$instance=new $class;
		$arr_info= $instance->$method($options);
		return $arr_info;
	}
	public static function __callStatic($method, $args) {
		$id=$args[0];
		$options=isset($args[1])?$args[1]:array();

		$class='Widget_'.$this->fixedId($id);
		$params=array_merge($this->getRequest()->getParams(),$options);
		$arr_info= $class::$method($options);
		return $arr_info;
	}

	/**
	 * Strategy pattern: call helper as broker method
	 *
	 * @param  string $name
	 * @param  array|Zend_Config $options
	 * @return Zend_Form
	 */
	public function direct ($name, $options = null)
	{
		return $this->loadWidget($name, $options);
	}
}